package artid.com.arttogo.activity;

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import artid.com.arttogo.R;
import artid.com.arttogo.app.ArtToGoApp;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.service.BluetoothServiceOne;
import artid.com.arttogo.service.BluetoothServiceTwo;
import artid.com.arttogo.utils.MySharedPreferences;


public class SettingActivity extends BaseActivity implements OnClickListener {
	private RelativeLayout mUpdatePass, mFindFriends;
	private Intent mIntent;
	private TextView mOutLogin, mTextname, mtextBackMusic;
	View grzl, zhgl;
	View backMusic;
	ImageView mIvPush;
	MySharedPreferences sharedPreferences;
	Boolean isArtPushOpen = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		mContext = SettingActivity.this;
		sharedPreferences = sharedPreferences
				.getMySharedPreferences(SettingActivity.this);
		ArtToGoApp.getInstance().addActivity(SettingActivity.this);
		initView();
	}

	private void initView() {
		mIvPush = (ImageView) findViewById(R.id.set_push);
		mIvPush.setOnClickListener(this);
		if (!sharedPreferences.getValue("isOpenBluetooth", "false").equals("true")) {
			isArtPushOpen = false;
			mIvPush.setImageResource(R.drawable.sz_btn_gray);
			
		}else {
			isArtPushOpen = true;
			mIvPush.setImageResource(R.drawable.sz_btn_green);
		}
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setVisibility(View.VISIBLE);
		mBack.setOnClickListener(this);
		zhgl = (View) findViewById(R.id.set_rela_user);
		zhgl.setOnClickListener(this);
		mtextBackMusic = (TextView) findViewById(R.id.text_backmusic);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		grzl = (View) findViewById(R.id.personal_data);
		grzl.setOnClickListener(this);
		mTextname = (TextView) findViewById(R.id.text_name);
		mTextname.setText(AppUtil.getUser().getRealname());
		mTittle.setText("设置");
		mUpdatePass = (RelativeLayout) findViewById(R.id.update_pass);
		mUpdatePass.setOnClickListener(this);
		mFindFriends = (RelativeLayout) findViewById(R.id.find_friends);
		mFindFriends.setOnClickListener(this);
		mOutLogin = (TextView) findViewById(R.id.out_login);
		mOutLogin.setOnClickListener(this);
		backMusic = (View) findViewById(R.id.view_backmusic);
		backMusic.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		int i = Integer.valueOf(sharedPreferences.getValue(sharedPreferences.TagMusicName, "0"));
		if (i == 2) {
			mtextBackMusic.setText("棹碧涛春水路");
		} else if (i == 1) {
			mtextBackMusic.setText("茉莉花");
		} else if (i == 0) {
			mtextBackMusic.setText("渔舟唱晚");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.set_push:
			if (isArtPushOpen) {
				mIvPush.setImageResource(R.drawable.sz_btn_gray);
				isArtPushOpen = false;
				Intent serviceOne = new Intent();
				serviceOne.setClass(mContext, BluetoothServiceOne.class);
				Intent serviceTwo = new Intent();
				serviceTwo.setClass(mContext, BluetoothServiceTwo.class);
				stopService(serviceOne);
				stopService(serviceTwo);
				sharedPreferences.setValue("isOpenBluetooth", "false");
				BluetoothAdapter bluetoothAdapter = BluetoothAdapter
				.getDefaultAdapter();
				if (bluetoothAdapter != null)
				{
					bluetoothAdapter.disable();
				}
			} else {
				mIvPush.setImageResource(R.drawable.sz_btn_green);
				isArtPushOpen = true;
				sharedPreferences.setValue("isOpenBluetooth", "true");
				Intent serviceOne = new Intent();
				serviceOne.setClass(mContext, BluetoothServiceOne.class);
				startService(serviceOne);
				Intent serviceTwo = new Intent();
				serviceTwo.setClass(mContext, BluetoothServiceTwo.class);
				startService(serviceTwo);
				
			}
			break;
		case R.id.view_backmusic:
			mIntent = new Intent(this, BackGroundMusicActivity.class);
			startActivity(mIntent);
			break;

		case R.id.update_pass:
			mIntent = new Intent(this, UpdatePasswordActivity.class);
			startActivity(mIntent);

			break;
		case R.id.find_friends:
			mIntent = new Intent(this, FindFriendsActivity.class);
			startActivity(mIntent);

			break;
		case R.id.out_login:

			AlertDialog alertDialog = new AlertDialog.Builder(
					SettingActivity.this)
					.setTitle("提示")
					.setMessage("请问您确定要退出吗？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									// TODO Auto-generated method stub

									mIntent = new Intent(SettingActivity.this,
											MainActivity.class);
									AppUtil.Logout(SettingActivity.this);
									startActivity(mIntent);
									ArtToGoApp.getInstance().exit();
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									// TODO Auto-generated method stub

								}
							}).show();

			break;
		case R.id.back_btn:
			finish();
			break;
		case R.id.personal_data:
			mIntent = new Intent(this, PersonalDataActivity.class);
			startActivity(mIntent);
			break;
		case R.id.set_rela_user:
			mIntent = new Intent(this, AccountMangActivity.class);
			startActivity(mIntent);
			break;
		default:
			break;
		}

	}

}
